package com.googlecode.gunncs.mechanism.gui;

import java.awt.Color;
import java.awt.Graphics;

import com.googlecode.grtframework.core.GRTLoggedProcess;
import com.googlecode.grtframework.vis.Displayable;
import com.googlecode.grtframework.vis.Displayer;
import com.googlecode.grtframework.vis.IMountedPosition;
import com.googlecode.grtframework.vis.Mountable;

/**
 * A DiffWheelGUI is a wheel visual.
 * 
 * @author ajc
 * 
 */
public class DiffWheelGUI extends GRTLoggedProcess implements Mountable,
		Displayable {

	private static final int WHEEL_DIAMETER = 5;

	private final IMountedPosition mount;
	private final Mountable frame;
	private final Displayer d;

	public DiffWheelGUI(Displayer d, IMountedPosition mount, Mountable frame) {
		super("wheel");// TODO
		this.d = d;
		this.mount = mount;
		this.frame = frame;
	}

	@Override
	public double getHeading() {
		return mount.getHeading();
	}

	@Override
	public int getX() {
		return mount.getX();
	}

	@Override
	public int getY() {
		return mount.getY();
	}

	/**
	 * Advances the position of the wheel by a provided distance. TODO units
	 * 
	 * @param distance
	 */
	public void addDistance(double distance) {
		double x = distance * Math.cos(mount.getHeading() + frame.getHeading());
		double y = distance * Math.sin(mount.getHeading() + frame.getHeading());
		mount.setPositionRelative((mount.getXRelative() + x),
				(mount.getYRelative() + y));
	}

	@Override
	public void display(Graphics g) {
		g.setColor(Color.DARK_GRAY);
		g.fillOval(mount.getX() - WHEEL_DIAMETER / 2, mount.getY()
				- WHEEL_DIAMETER / 2, WHEEL_DIAMETER, WHEEL_DIAMETER);

	}

	@Override
	public synchronized void initiate() {
		super.initiate();
		d.addDisplayable(this);
	}

	@Override
	public synchronized void terminate() {
		super.terminate();
		d.removeDisplayable(this);
	}

}
